package org.crumbleworks.crumbchat.client;

import java.io.IOException;

import org.crumbleworks.crumbchat.common.Network;
import org.crumbleworks.crumbchat.common.data.Message;
import org.crumbleworks.crumbchat.common.data.UserData;

import com.esotericsoftware.kryonet.Client;

public class ChatClient extends Client {
    private int tcpPort, udpPort, timeout;
    private String host;

    private final int DEFAULT_TCPPORT = 44444;
    private final int DEFAULT_UDPPORT = 44444;
    private final int DEFAULT_TIMEOUT = 5000;

    public ChatClient() {
        this.tcpPort = DEFAULT_TCPPORT;
        this.udpPort = DEFAULT_UDPPORT;
        this.timeout = DEFAULT_TIMEOUT;
        
        init();
    }
    
    public ChatClient(String host) {
        this.host = host;
        this.tcpPort = DEFAULT_TCPPORT;
        this.udpPort = DEFAULT_UDPPORT;
        this.timeout = DEFAULT_TIMEOUT;
        
        init();
    }
    
    public ChatClient(String host, int tcpPort) {
        this.host = host;
        this.tcpPort = tcpPort;
        this.udpPort = DEFAULT_UDPPORT;
        this.timeout = DEFAULT_TIMEOUT;
        
        init();
    }
    
    public ChatClient(String host, int tcpPort, int udpPort) {
        this.host = host;
        this.tcpPort = tcpPort;
        this.udpPort = udpPort;
        this.timeout = DEFAULT_TIMEOUT;
        
        init();
    }
    
    public ChatClient(String host, int tcpPort, int udpPort, int timeout) {
        this.host = host;
        this.tcpPort = tcpPort;
        this.udpPort = udpPort;
        this.timeout = timeout;
        
        init();
    }

    private void init() {
        start();
        Network.register(this);
    }

    public void connect() throws IOException {
        connect(timeout, host, tcpPort, udpPort);
    }
    
    public void disconnect() {
        close();
    }

    public void sendUserData(UserData userData) {
        sendTCP(userData);
    }
    
    public void sendMessage(Message message) {
        sendUDP(message);
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
}
